<?php
	
	class Database {
		private $mysqli = null;
		
		/**
		 * @param DBConfig $config database config
		 * @return boolean
		 */
		public function Connect(DBConfig $config) {
			$this->mysqli = new mysqli($config->m_host, $config->m_user, $config->m_pass, $config->m_db);
			
			if ($this->mysqli->connect_error) {
				throw new Exception($this->mysqli->connect_error);
			}
			
			$this->mysqli->set_charset("utf8");
			
			return true;			
		}
		
		/**
		 * Select a single value from a query
		 * @param string $sqlQuery SQL query with one parameter output
		 * @return
		 */
		public function SelectOne($stmt) {
			
			if ($stmt == false) {
				throw new Exception($this->mysqli->error);
			}
			
			if ($stmt->execute() == false) {
				throw new Exception($this->mysqli->error);
			}
			
			$ret = 0;
			
			if ($stmt->bind_result($ret) == false) {
				throw new Exception($this->mysli->error);
			}
			
			$stmt->fetch();
			
			return $ret;
		}
		
		/**
		 * Select several values from a query
		 * @param $sqlQuery
		 * @return $stmt
		 */
		public function Select($stmt) {
			$retArray;
			
			if ($stmt === false) {
				throw new Exception($this->mysqli->error);
			}
			
			if ($stmt->execute() == false) {
				throw new Exception($this->mysqli->error);
			}
			
			$ret;
			
			if ($stmt->bind_result($ret) == false) {
				throw new Exception($this->mysli->error);
			}
			
			while ($stmt->fetch()) {
				$retArray[] = $ret; 
			}
			
			return $retArray;
		}
		
		/**
		 * Prepares a query
		 * @param string $sql Sql query
		 * @return mysqli_stmt
		 */
		public function Prepare($sql) {
			$ret = $this->mysqli->prepare($sql);
			
			if ($ret == false) {
				throw new Exception($this->mysqli->error);
			}
			
			return $ret;
		}
		
		/**
		 * @param mysqli_stmt $stmt
		 * @return integer insert id
		 */
		public function RunInsertQuery(mysqli_stmt $stmt) {
			if ($stmt->Execute() == false) {
				throw new Exception($this->mysqli->error);
			}
			
			$ret = $stmt->insert_id;

			return $ret;
		}
		
		/**
		 * Close connection
		 */
		public function Close() {
			return $this->mysqli->close();
		}
		
		/**
		 * @param DBConfig $dbConfig
		 * @return test data
		 */
		public static function test(DBConfig $dbConfig) {
			$db = new Database();
			
			if ($db->Connect($dbConfig) == false) {
				echo "Database Connect failed";
				return false;
			}
			
			$numberOfPostBefore = $db->SelectOne("SELECT COUNT(*) FROM InsertTable");
			
			$stmt = $db->Prepare("INSERT INTO InsertTable VALUES (1)");
			$db->RunInsertQuery($stmt);
			
			$numberOfPostAfter = $db->SelectOne("SELECT COUNT(*) FROM InsertTable");
			
			if ($numberOfPostBefore +1 != $numberOfPostAfter) {
				echo "Prepare or RunInsertQuery failed";
				return false;
			}
			
			if ($db->SelectOne("SELECT COUNT(*) FROM NotEmpty") != 2) {
				echo "Database SelectOne failed";
				return false;
			}
			
			if ($db->Close() == false) {
				echo "Database Close failed";
				return false;
			}
			
			
			return true;
		}
	}
?>